/**
 * Meituan.com Inc.
 * Copyright (c) 2010-2018 All Rights Reserved.
 */
package priv.tuyou.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>线程安全的时间工具类</p>
 *
 * @author tuyou@meituan.com
 * @version $Id: DateUtil.java, v 0.1 2018-01-27 下午3:48 tuyou $$
 */
public class DateUtil {

    public static final String pattern_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    public static final String pattern_yyyy_MM_dd = "yyyy-MM-dd";
    public static final String pattern_yyyyMMdd = "yyyyMMdd";
    public static final String pattern_yyMMdd = "yyMMdd";
    public static final String pattern_HH_mm_ss = "HH:mm:ss";
    public static final String pattern_HH_mm_ss_SSS = "HH:mm:ss.SSS";

    private static final ThreadLocal<SimpleDateFormat> formator_yyyy_MM_dd_HH_mm_ss = new ThreadLocal<SimpleDateFormat>();
    private static final ThreadLocal<SimpleDateFormat> formator_yyyy_MM_dd = new ThreadLocal<SimpleDateFormat>();
    private static final ThreadLocal<SimpleDateFormat> formator_yyMMdd = new ThreadLocal<SimpleDateFormat>();
    private static final ThreadLocal<SimpleDateFormat> formator_HH_mm_ss = new ThreadLocal<SimpleDateFormat>();
    private static final ThreadLocal<SimpleDateFormat> formator_HH_mm_ss_SSS = new ThreadLocal<SimpleDateFormat>();
    private static final ThreadLocal<SimpleDateFormat> formator_yyyyMMdd = new ThreadLocal<SimpleDateFormat>();

    private DateUtil() {
    }

    /**
     * 获取日期为昨天的java.sql.Date对象
     *
     * @return
     */
    public static java.sql.Date getYesterdaySqlDate() {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_YEAR, -1);
        return new java.sql.Date(now.getTime().getTime());
    }

    /**
     * 解析完整的【日期+时间】字符串
     *
     * @param dateStr
     * @return
     */
    public static Date parseDateTime(String dateStr) {
        SimpleDateFormat simpleDateFormat = getAndCheck(formator_yyyy_MM_dd_HH_mm_ss, pattern_yyyy_MM_dd_HH_mm_ss);
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 格式化未完整的【日期+时间】字符串
     *
     * @param date
     * @return
     */
    public static String formatDateTime(Date date) {
        SimpleDateFormat simpleDateFormat = getAndCheck(formator_yyyy_MM_dd_HH_mm_ss, pattern_yyyy_MM_dd_HH_mm_ss);
        return simpleDateFormat.format(date);
    }

    /**
     * 解析只包含【日期】的字符串
     *
     * @param dateStr
     * @return
     */
    public static Date parseDate(String dateStr) {
        SimpleDateFormat simpleDateFormat = getAndCheck(formator_yyyy_MM_dd, pattern_yyyy_MM_dd);
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 格式化为只包含【日期】的字符串，并且没有分隔符
     *
     * @param date
     * @return
     */
    public static String formatTo_yyyyMMdd(Date date) {
        SimpleDateFormat simpleDateFormat = getAndCheck(formator_yyyyMMdd, pattern_yyyyMMdd);
        return simpleDateFormat.format(date);
    }

    /**
     * 格式化时间部分
     *
     * @param date
     * @return
     */
    public static String formatTime(Date date) {
        SimpleDateFormat simpleDateFormat = getAndCheck(formator_HH_mm_ss, pattern_HH_mm_ss);
        return simpleDateFormat.format(date);
    }

    /**
     * 格式化日期部分，年份是显示后两位，2018只显示18
     *
     * @param date
     * @return
     */
    public static String formatTo_yyMMdd(Date date) {
        SimpleDateFormat simpleDateFormat = getAndCheck(formator_yyMMdd, pattern_yyMMdd);
        return simpleDateFormat.format(date);
    }

    /**
     * 格式化时间部分，并带上毫秒
     *
     * @param date
     * @return
     */
    public static String formatTimes(Date date) {
        SimpleDateFormat simpleDateFormat = getAndCheck(formator_HH_mm_ss_SSS, pattern_HH_mm_ss_SSS);
        return simpleDateFormat.format(date);
    }

    public static String formatDate(Date date) {
        SimpleDateFormat simpleDateFormat = getAndCheck(formator_yyyy_MM_dd, pattern_yyyy_MM_dd);
        return simpleDateFormat.format(date);
    }

    /**
     * 得到SimpleDateFormat，会检查是否set了
     *
     * @param local   哪个ThreadLocal变量
     * @param pattern 格式化模板
     * @return
     */
    private static SimpleDateFormat getAndCheck(ThreadLocal<SimpleDateFormat> local, String pattern) {
        SimpleDateFormat simpleDateFormat = local.get();
        if (simpleDateFormat == null) {
            synchronized (DateUtil.class) {
                if (local.get() == null) {
                    simpleDateFormat = new SimpleDateFormat(pattern);
                    local.set(simpleDateFormat);
                }
            }
        }
        return simpleDateFormat;
    }

    /**
     * 计算加减指定天数
     *
     * @param curDate
     * @param addDays
     * @return
     */
    public static Date addDays(Date curDate, int addDays) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(curDate);
        calendar.add(Calendar.DAY_OF_YEAR, addDays);
        return calendar.getTime();
    }
}